#!/usr/bin/perl

# This script displays a table of all the calibrators we know about, along with their RA, Dec,
# and the latest flux value we have for them, closest to the recommended frequency in each band.

use CGI qw(:standard);
use DBI;

use strict; use warnings;

my $buffer;

# read in the parameters
$ENV{'REQUEST_METHOD'}=~tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET"){
    $buffer=$ENV{'QUERY_STRING'};
}
# split information into name/value pairs
my @pairs=split(/\&/,$buffer);
my %FORM;
foreach my $pair (@pairs) {
    my ($name,$value)=split(/\=/,$pair);
    $value=~tr/+/ /;
    $value=~s/%(..)/pack("C",hex($1))/eg;
    $FORM{$name}=$value;
}

print header;
print "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/stylesheets/ca_style.css\">\n";
print "<title>";
print "Information on ".$FORM{'source'};
print "</title>";
print "<script type=\"text/javascript\" src=\"/swfobject.js\"></script>\n";
print "<script type=\"text/javascript\">swfobject.embedSWF(\"/open-flash-chart.swf\",".
    "\"my_chart\",\"800\",\"300\",\"9.0.0\",\"expressInstall.swf\",".
    "{\"data-file\":\"/cgi-bin/Calibrators/scatter_calplot.pl?data=".$FORM{'source'}."_".
    $FORM{'freq'}."\"});</script>\n";
print "</head><body>\n";

# database details
my $dsn="dbi:Pg:dbname=atca;host=mentok;port=5432";
my $user="postgres";
my $password="";

# Setup the database connection
my $dbh=DBI->connect($dsn,$user,$password,{ RaiseError => 0, AutoCommit => 1});

print "<center><h1>Plot of ".$FORM{'source'}." \@ ".$FORM{'freq'}." MHz</h1></center><br />\n";

print "<center><div id=\"my_chart\"></div></center>\n";


$dbh->disconnect;

print "</body></html>\n";

